package cloud.take.off.service.order.impl;

import cloud.take.off.api.system.UserServiceFeign;
import cloud.take.off.mapper.order.BaseOrderMapper;
import cloud.take.off.module.common.R;
import cloud.take.off.module.order.dto.BaseOrderDTO;
import cloud.take.off.module.order.entity.BaseOrder;
import cloud.take.off.module.order.entity.OrderItem;
import cloud.take.off.module.order.vo.BaseOrderVO;
import cloud.take.off.module.order.vo.OrderItemVO;
import cloud.take.off.module.system.query.UserInfoDTO;
import cloud.take.off.module.system.vo.UserInfoVO;
import cloud.take.off.mybatis.base.BaseServiceImpl;
import cloud.take.off.mybatis.support.Condition;
import cloud.take.off.service.order.IBaseOrderService;
import cloud.take.off.service.order.IOrderItemService;
import cloud.take.off.wrapper.BaseOrderWrapper;
import cloud.take.off.wrapper.OrderItemWrapper;
import cn.hutool.core.collection.CollectionUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
 * @Description: TODO
 * @Author: Take-off
 * @Date: 2024/10/9 10:50 AM
 * @Param
 * @return
 * @Version: 1.0
 **/
@Service
@AllArgsConstructor
public class BaseOrderServiceImpl extends BaseServiceImpl<BaseOrderMapper,BaseOrder> implements IBaseOrderService {

    public final IOrderItemService orderItemService;

    public final BaseOrderWrapper baseOrderWrapper;

    public final OrderItemWrapper orderItemWrapper;

    public final UserServiceFeign userServiceFeign;

    @Override
    public boolean submit(BaseOrderVO baseOrderVO){
        BaseOrder baseOrder = baseOrderWrapper.voToEntity(baseOrderVO);
        boolean result = saveOrUpdate(baseOrder);
        if (result){
            List<OrderItemVO> orderItemVOS = baseOrderVO.getOrderItemVOS();
            if (CollectionUtil.isNotEmpty(orderItemVOS)){
                for (OrderItemVO orderItemVO:orderItemVOS){
                    OrderItem orderItem = orderItemWrapper.voToEntity(orderItemVO);
                    orderItem.setOrderId(baseOrder.getId());
                    orderItemService.saveOrUpdate(orderItem);
                }
            }
            return true;
        }else {
            return false;
        }
    }

    @Override
    public BaseOrderVO detail(BaseOrderDTO dto){
        BaseOrderVO baseOrderVO = new BaseOrderVO();
        BaseOrder baseOrder = getOne(Condition.getQueryWrapper(baseOrderWrapper.dtoToEntity(dto)));
        if (Optional.ofNullable(baseOrder).isPresent()){
            baseOrderVO = baseOrderWrapper.entityToVO(baseOrder);
            //子项
            List<OrderItem> orderItems = orderItemService.listByOrderId(baseOrder.getId());
            List<OrderItemVO> orderItemVOS = orderItemWrapper.entityToVO(orderItems);
            baseOrderVO.setOrderItemVOS(orderItemVOS);
            //用户姓名
            UserInfoDTO infoDTO = new UserInfoDTO();
            infoDTO.setId(baseOrderVO.getUserId());
            R<UserInfoVO> userInfoVOR = userServiceFeign.detail(infoDTO);
            if (userInfoVOR.isSuccess()){
                String userName = userInfoVOR.getPageData().getName();
                baseOrderVO.setUserName(userName);
            }
        }
        return baseOrderVO;
    }

    @Override
    public boolean deleteLogic(List<Long> ids){
        boolean result = super.deleteLogic(ids);
        if (result){
            orderItemService.deleteByOrderId(ids);
        }
        return result;

    }

}
